package com.epi.two;

import java.sql.*;

/**
 * @author dxy
 * @version 1.0
 */
public class SQLDemo {
    public static void main(String[] args) {
        // JDBC 连接信息
        String url = "jdbc:mysql://localhost:3306/epi?useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "root";

        // 加载驱动（可选，JDK 6+ 可省略）
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        // 使用 try-with-resources 自动释放资源
        // JNDI 可以代替 DriverManager的硬编码
        try (
            Connection conn = DriverManager.getConnection(url, username, password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT id, username, email FROM sys_user");
        ) {
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String email = rs.getString("email");
                System.out.printf("ID: %d, Name: %s, Email: %s%n", id, name, email);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
